use std::fs;

use bindgen::Builder;
use cc::Build;

const RTOS_PATH: &'static str = "./cmsis_rtos2";

fn bind_header() {
    let bind = Builder::default()
    .header("./cmsis_rtos2/rtx_os.h")
    .use_core()
    .generate().unwrap();
    bind.write_to_file("./rtx_os.rs").unwrap();
}

fn main() {
    bind_header();
    // CC_thumbv7m-none-eabi
    // gcc  https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu-rm/10.3-2021.10/gcc-arm-none-eabi-10.3-2021.10-win32.zip
    let mut build = Build::new();
    build.define("STM32F10X_MD", "");
    build.include("./cmsis_rtos2");
    build.include("./cmsis_rtos2/CMSIS");
    build.include("./cmsis_rtos2/device");
    
    let dir = fs::read_dir(RTOS_PATH).unwrap();
    
    for file in dir.into_iter() {
        let file = file.unwrap();
        if file.file_type().unwrap().is_dir() {
            continue;
        }
        let name = file.file_name().into_string().unwrap();
        if name.ends_with(".h") {
            continue;
        }
      //  println!("cargo:warning=name:{}", name);
        let path = format!("{}/{}", RTOS_PATH, name);
        build.file(path);    
    }
    build.file("./cmsis_rtos2/gcc/irq_armv7m.S");
    build.file("./cmsis_rtos2/device/system_stm32f10x.c");
    build.try_compile("cmsis_rtos2").unwrap();
}
